define(function(require) {
	var WebUploader = require('WebUploader');
	var layer = require('layer');
	var appConst = require('appConst');
	
	var WebuploaderUtil = {
		fileSingleSizeLimit : 1024 * 1024 * 10, //单文件限制在10M
		accept : {
			title : 'doc,docx,xls,xlsx,pdf,jpg,jpeg,png,gif,tif,txt,rar,zip',
			extensions : 'doc,docx,xls,xlsx,pdf,jpg,jpeg,png,gif,tif,txt,rar,zip',
			mimeTypes : 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
					+ ',application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
					+ ',application/pdf,image/jpeg,image/png,image/tiff,.gif,.tif,.txt,.rar,.zip'
		},
		error : function(handler) {
			if ('F_EXCEED_SIZE' == handler) {
				layer.msg('文件大小不能超过10M');
			} else if ('Q_TYPE_DENIED' == handler) {
				layer.msg('不支持该文件类型上传');
			}
		},
		getDefaultsOpts : function(options) {
			var self = this;
			var opts = {
				// swf文件路径
				swf : appConst.path + '/core/js/webuploader/Uploader.swf',
				// 文件接收服务端。
				server : appConst.path + '/file/upload.do',
				pick : {
					multiple : true
				},
				auto : true,
				duplicate : true,
				fileSingleSizeLimit : self.fileSingleSizeLimit,
				accept : self.accept
			};
			
			if(typeof options=='string' || options instanceof jQuery){
				opts.pick.id = options;
			}else{
				opts = $.extend({}, $.extend({}, opts),options);
			}
			return opts;
		},
		create : function(options){
			var webuploaderOpts = this.getDefaultsOpts(options);
			var uploader = WebUploader.create(webuploaderOpts);
			uploader.on('error', this.error);
			uploader.on('beforeFileQueued', function(file){
				if(file.size==0){
					layer.msg('不允许空文件');
					return false;
				}
				if(file.name.length>220){
					layer.msg('文件名不能超过220字符');
					return false;
				}
			});
			return uploader;
		}
	}
	
	return WebuploaderUtil;
});